<template>
  <div style="padding-bottom: 50px">
    <!-- 搜索框 -->
    <van-search
      shape="round"
      background="#c82519"
      placeholder="请输入搜索关键词"
    />
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <!-- 轮播图 -->
      <van-swipe
        style="height: 200px; width: 100%"
        :autoplay="3000"
        indicator-color="white"
      >
        <van-swipe-item v-for="item in bannerList" :key="item.bannerid">
          <img style="width: 100%; height: 100%" :src="item.img" />
        </van-swipe-item>
      </van-swipe>
      <!-- 菜单列表 -->
      <van-grid :column-num="5" :border="false">
        <van-grid-item
          v-for="item in menuList"
          :key="item.id"
          :icon="item.icon"
          :text="item.title"
        />
      </van-grid>
      <!-- 商品列表 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getGoodsListData"
        class="goods-list"
      >
        <!-- 声明式导航 -->
        <!-- <router-link
          class="goods-item"
          v-for="item in goodsList"
          :key="item.proid"
          :to="`/detail?proid=${item.proid}`"
        > -->
        <!-- 编程式导航 -->
        <div
          class="goods-item"
          v-for="item in goodsList"
          :key="item.proid"
          @click="$router.push(`/detail?proid=${item.proid}`)"
        >
          <img :src="item.img1" />
          <!-- <img :src="item.img1" @error="loadDefaultPicture" /> -->
          <!-- <img v-img="item.img1" /> -->
          <span class="title">{{ item.proname }}</span>
          <span class="desc">{{ item.desc }}</span>
          <span class="price">￥{{ formatPrice(item.originprice) }}</span>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { getBannerList, getMenuList, getGoodsList } from '@/api/home'
import { reactive } from 'vue'

const bannerList = ref([])
const menuList = ref([])
const goodsList = ref([])

// 上拉 & 下拉相关
const goodsQueryObj = reactive({ count: 0, limitNum: 10 })
const isLoading = ref(false)
const finished = ref(false)
const loading = ref(false)

const onRefresh = () => {}

// 获取轮播图列表数据
const getBannerListData = async () => {
  const res = await getBannerList()
  bannerList.value = res.data
}

// 获取菜单列表数据
const getMenuListData = async () => {
  const res = await getMenuList()
  menuList.value = res.data
}

// 获取商品列表
const getGoodsListData = async () => {
  goodsQueryObj.count++
  const res = await getGoodsList(goodsQueryObj)
  goodsList.value = [...goodsList.value, ...res.data]

  if (res.data.length === 0) {
    // 没有数据啦
    finished.value = true
  }

  // 一定要把这个属性设置为false，否则滚动到下面就不会加载了
  loading.value = false
  // 数据加载完毕，把下刷刷新状态，设置为false
  isLoading.value = false
}

// 格式化金额
const formatPrice = price => {
  return price.toFixed(2)
}

getBannerListData()
getMenuListData()
</script>

<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  box-sizing: border-box;
  width: 100%;

  .goods-item {
    box-sizing: border-box;
    background-color: #fff;
    padding: 5.33333vw;
    border-radius: 1.33333vw;
    display: flex;
    align-items: center;
    width: 48%;
    flex-direction: column;
    margin-top: 1.33333vw;
    img {
      width: 26.66667vw;
      height: 32vw;
    }
    .title {
      font-size: 3.73333vw;
      display: inline-block;
      white-space: nowrap;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-top: 4vw;
      color: #000;
    }
    .desc {
      font-size: 3.2vw;
      color: #c3c3c3;
      display: inline-block;
      white-space: nowrap;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-top: 2.66667vw;
    }
    .price {
      font-size: 3.2vw;
      margin-top: 2.66667vw;
      color: red;
    }
  }
}
// 深度作用选择器
::v-deep .van-list__finished-text {
  width: 100%;
  text-align: center;
}
</style>
